package com.fg.cookie;

import static com.fg.security.LocalEncrypter.decrypt;
import static com.fg.security.LocalEncrypter.encrypt;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Cookie login class
 * @author Joseph Choi
 *
 */
public class CookieInfo {
	public static final String LOGIN_COOKIE_NAME = "freegive_id";
	public static final String EM_COOKIE_NAME = "freegive_em";
	public static final String TIMEZONE_COOKIE_NAME = "freegive_timezone";
	
	public static final int NOT_LOGIN = -1;
	
	/**
	 * 쿠키에 로그인 정보를 남긴다.
	 * member_seq를 암호화해서 남긴다.
	 * @param user_seq 로그인 정보가 담긴 Member객체의 member_seq 
	 * @param response 쿠키가 반영될 HttpResponse객체 
	 * @throws Exception
	 */
	public static void loginByCookie(int member_seq, HttpServletResponse response) throws Exception {
		Cookie cookie = new Cookie(LOGIN_COOKIE_NAME, 
				encrypt(Integer.toString(member_seq)));
		cookie.setMaxAge(-1); //브라우저 종료시 같이 종료
		//cookie.setMaxAge(30*24*60*60);//30일동안 유효
		response.addCookie(cookie);
	}
	
	/**
	 * @Comment	: 쿠키에 로그인 정보를 남긴다.
	 * email을 암호화해서 남긴다.
	 * @param email
	 * @param response
	 * @throws Exception
	 */
	public static void writeEmailByCookie(String email, HttpServletResponse response) throws Exception {
		Cookie cookie = new Cookie(EM_COOKIE_NAME, 
				encrypt(email));
		//cookie.setMaxAge(-1);
		cookie.setMaxAge(30*24*60*60);//30일동안 유효
		response.addCookie(cookie);
	}
	
	/**
	 * 이메일정보를 쿠키에서 읽어온다.
	 * email을 복호화해서 가져온다.
	 * @param request 쿠키를 읽어올 HttpRequest 객체
	 * @return 로그인할 String타입 email, 만약 쿠키에 로그인 정보가 없으면 -1(NOT_LOGIN) 리턴 
	 * @throws Exception
	 */
	public static String getEmailFromCookie(HttpServletRequest request) throws Exception{
		String email = null;
		Cookie[] cookies = request.getCookies();
		
		if (cookies == null) return null;
		
		for (int i = 0; i < cookies.length; i++) {
			Cookie cookie = cookies[i];
			if (cookie.getName().equals(EM_COOKIE_NAME)) {
				email = cookie.getValue();
			}
		}
		
		if (email != null) {
			email = decrypt(email);
			if (email == null) {
				return null;
			}
			return email;
		}
		
		else {
			return null;
		}
	}
	
	
	/**
	 * 로그인정보를 쿠키에서 읽어온다.
	 * member_seq를 복호화해서 가져온다.
	 * @param request 쿠키를 읽어올 HttpRequest 객체
	 * @return 로그인된 Integer타입 member_seq, 만약 쿠키에 로그인 정보가 없으면 -1(NOT_LOGIN) 리턴 
	 * @throws Exception
	 */
	public static Integer getLoginSeqFromCookie(HttpServletRequest request) throws Exception{
		String loginSeq = null;
		Cookie[] cookies = request.getCookies();
		
		if (cookies == null) return null;
		
		for (int i = 0; i < cookies.length; i++) {
			Cookie cookie = cookies[i];
			if (cookie.getName().equals(LOGIN_COOKIE_NAME)) {
				loginSeq = cookie.getValue();
			}
		}
		
		if (loginSeq != null) {
			loginSeq = decrypt(loginSeq);
			if (loginSeq == null) {
				return null;
			}
			return Integer.parseInt(loginSeq);
		}
		
		else {
			return null;
		}
	}
	
	/**
	 * 로그아웃한다.
	 * 동일한 쿠키를 생성하여 쿠키의 expire 시간을 지금으로 설정한다.(바로삭제)
	 * @param response 쿠키가 반영될 HttpRespose객체 
	 */
	public static void logoutByCookie(HttpServletResponse response, HttpServletRequest reqeust) {
		Cookie[] cookie = reqeust.getCookies();
		for(int i=0 ; i < cookie.length ; i++){
			if (cookie[i].getName().equals(EM_COOKIE_NAME)) {
				cookie[i].setMaxAge(30*24*60*60);//30일동안 유효
				response.addCookie(cookie[i]);
			}else{
				cookie[i].setMaxAge(0);//바로삭제
				response.addCookie(cookie[i]);
			}
		}
	}
	
}
